{{Extend "layout"}}
{{Block "title"}}{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}</li>
{{/Block}}
{{Block "main"}}
<div class="row">
    <div class="col-md-12">
        <div class="block-flat">
          <div class="header">							
            <h3>{{if HasSuffix URL.Path "_add"}}{{"添加网站"|T}}{{else}}{{"修改网站"|T}}{{end}}</h3>
          </div>
          <div class="content">
              <form class="form-horizontal group-border-dashed" method="POST" action="">
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"网址/域名/IP"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" name="domain" class="form-control" required="required" value="{{Form "domain"}}">
                    <div class="help-block">{{"网站的网址、域名或IP，支持通配符“*”(例如*.admpub.com)和环境变量(环境变量用花括号括起来，例如localhost:{$PORT})。如有多个，用空格隔开"|T}}</div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"物理路径"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" name="root" class="form-control" required="required" value="{{Form "root"}}">
                    <div class="help-block">{{"在服务器上的绝对路径"|T}}</div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"日志文件"|T}}</label>
                <div class="col-sm-8">
                    <input type="text" name="log" class="form-control" value="{{Val "log" "access_local.log"}}">
                    <div class="help-block">{{"日志文件保存路径"|T}}</div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"Header"|T}}</label>
                <div class="col-sm-8">
                  <div class="fieldset bg-6e">
                    <div class="form-group">
                          <label class="col-sm-2 control-label">{{"指定路径"|T}}</label>
                          <div class="col-sm-9">
                             <input type="text" class="form-control" name="header_path" value="{{Form "header_path"}}">
                             <div class="help-block">{{"可以针对某个起始路径设置header"|T}}</div>
                          </div>
                    </div>

                    <div class="form-group">
                          <label class="col-sm-2 control-label">{{"设置Header"|T}}</label>
                          <div class="col-sm-9">
                    <table>
								        <thead>
								        	<tr>
								        		<th style="width:50%;">{{"字段名"|T}}</th>
								        		<th>{{"值"|T}}</th>
								        		<th width="70">{{"删除"|T}}</th>
								        	</tr>
								        </thead>
								        <tbody>
                                {{$vals := FormValues "header_v"}}
                                {{$vlen := len $vals}}
                                {{range $k,$v:=FormValues "header_k"}}
                                    {{if lt $k $vlen}}
<tr>
	<td><input type="text" class="form-control" name="header_k" value="{{$v}}"></td>
	<td><input type="text" class="form-control" name="header_v" value="{{index $vals $k}}"></td>
	<td class="text-center">
    <button type="button" class="btn btn-danger" onclick="$(this).parent().parent().remove()">
      <i class="fa fa-times"></i>
    </button>
  </td>
</tr>
                                        
                                    {{end}}
                                {{end}}
								        	<tr>
								        		<td colspan="3" class="text-center primary-emphasis" style="cursor:pointer" id="headerAddVarBtn"><i class="fa fa-plus"></i></td>
								        	</tr>
								        </tbody>
							      </table>
                    <div class="help-block">{{"如果要将已存在的header删掉，请在名称前使用减号(-)并将其值留空"|T}}</div>
                  </div>
                  
                          </div>
                    </div>

                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"HTTPS"|T}}</label>
                <div class="col-sm-8">
                    {{$v := Form "tls"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="tls" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="tls" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="tls-settings">
                        {{if eq $v "1"}}{{Include "addon/form/tls"}}{{end}}
                    </div>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"GZIP"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "gzip"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="gzip" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="gzip" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="gzip-settings">
                        {{if eq $v "1"}}{{Include "addon/form/gzip"}}{{end}}
                    </div>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"FastCGI"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "fastcgi"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="fastcgi" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="fastcgi" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="fastcgi-settings">
                        {{if eq $v "1"}}{{Include "addon/form/fastcgi"}}{{end}}
                    </div>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"Proxy"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "proxy"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="proxy" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="proxy" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="proxy-settings">
                        {{if eq $v "1"}}{{Include "addon/form/proxy"}}{{end}}
                    </div>
                </div>
              </div>

              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"文件服务"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "browse"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="browse" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="browse" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="browse-settings">
                        {{if eq $v "1"}}{{Include "addon/form/browse"}}{{end}}
                    </div>
                </div>
              </div>

              <div class="form-group">
                <label class="col-sm-2 control-label">{{"静态文件缓存"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "expires"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="expires" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="expires" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="expires-settings">
                        {{if eq $v "1"}}{{Include "addon/form/expires"}}{{end}}
                    </div>
                </div>
              </div>
              
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"IP过滤"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "ipfilter"}}
                    <label class="radio-inline">
                      <input type="radio" value="1" name="ipfilter" class="icheck" data-slide-settings="show"{{if eq $v "1"}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="0" name="ipfilter" class="icheck" data-slide-settings="hide"{{if or (eq $v "0") (eq $v "")}} checked{{end}}> {{"禁用"|T}}
                    </label>
                    <div class="fieldset bg-6e hide" id="ipfilter-settings">
                        {{if eq $v "1"}}{{Include "addon/form/ipfilter"}}{{end}}
                    </div>
                </div>
              </div>

<div class="form-group">
  <label class="col-sm-2 control-label">{{"网址重写"|T}}</label>
  <div class="col-sm-8">
     <textarea type="text" id="rewrite_rule" class="form-control" style="min-height:200px" name="rewrite_rule">{{Form "rewrite_rule"}}</textarea>
     <div class="help-block">
         语法：<a href="javascript:;" onclick="App.insertAtCursor($('#rewrite_rule')[0],'rewrite from to\n');"><strong>rewrite</strong> <em style="color:green">from</em> <em style="color:green">to</em></a><br />
         高级语法：<a href="javascript:;" onclick="insertAdvRewrite()">[{{"插入"|T}}]</a><br />
<style>
.adv-rewrite-rule [data-popover]{border-bottom:1px dashed #aaa;cursor:help;}
</style>
<pre class="adv-rewrite-rule">
<strong>rewrite</strong> <em style="color:green" data-html="true" data-popover="popover" data-content="{{"正则(网址重写之前匹配的路径)<br />默认为“/”"}}" title="basepath">[basepath]</em> {
    <strong>regexp</strong> <span data-html="true" data-popover="popover" data-content="{{"匹配规则。<br />支持正则表达式。<br />对于高负载的网站应该尽量避免使用正则表达式"}}" title="pattern">pattern</span>
    <strong>ext</strong>    <span data-html="true" data-popover="popover" data-content="{{"带点号的扩展名(多个用空格隔开)<br />带前缀“!”则表示排除，如果设为“/”则表示没有扩展名"}}" title="extensions...">extensions...</span>
    <strong>if</strong>     <span data-html="true" data-popover="popover" data-content="{{"判断条件。<br /><strong>a</strong>和<strong>b</strong>可以是字符串或<a href='javascript:;' onclick='showRequestPlaceholders()'>占位符</a>；<br /><strong>cond</strong>是条件，支持以下条件：<p><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>is</strong></a> - 等于<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not</strong></a> - 不等于<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>has</strong></a> - 包含子字符串<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_has</strong></a> - 不包含子字符串<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>starts_with</strong></a> - 带前缀<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>ends_with</strong></a> - 带后缀<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>match</strong></a> - 匹配正则表达式<br /><a href='javascript:;' onclick='insertCondForRewrite(this)'><strong>not_match</strong></a> - 不匹配正则表达式<br /></p>"}}" title="a cond b">a cond b</span>
    <strong>if_op</strong>  <em style="color:green" data-html="true" data-popover="popover" data-content="{{"if条件之间的逻辑关系(默认是and)"}}" title="and|or">[and|or]</em>
    <strong>to</strong>     <span data-html="true" data-popover="popover" data-content="{{"网址替换规则。<br />可以是一个或多个以空格隔开的路径；<br />支持<a href='javascript:;' onclick='showRequestPlaceholders()'>占位符</a>；<br />支持正则捕获编号(比如：{1},{2}等)"}}" title="destinations...">destinations...</span>
}
</pre>
     </div>
  </div>
</div>

              <div class="form-group">
                <label class="col-sm-2 control-label">{{"备注"|T}}</label>
                <div class="col-sm-8">
                  <textarea class="form-control" name="remark">{{Form "remark"}}</textarea>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"网站状态"|T}}</label>
                <div class="col-sm-8">
                  {{$v := Form "disabled"}}
                  <label class="radio-inline">
                      <input type="radio" value="N" name="disabled" class="icheck"{{if or (eq $v "N") (eq $v "")}} checked{{end}}> {{"启用"|T}}
                    </label>
                    <label class="radio-inline">
                      <input type="radio" value="Y" name="disabled" class="icheck"{{if eq $v "Y"}} checked{{end}}> {{"禁用"|T}}
                    </label>
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">{{"是否重启"|T}}</label>
                <div class="col-sm-8">
                  <label class="checkbox-inline">
                  <input type="checkbox" class="icheck" name="restart" value="1" /> {{"保存并重启服务器"|T}} 
                  </label>
                </div>
              </div>

              <div class="form-group">
									<div class="col-sm-9 col-sm-offset-2">
									  <button type="submit" class="btn btn-primary btn-lg"><i class="fa fa-save"></i> {{"保存"|T}}</button>
									  <button type="reset" class="btn btn-default btn-lg"><i class="fa fa-refresh"></i> {{"重置"|T}}</button>
									</div>
							</div>
            </form>
          </div><!-- /.content -->
        </div><!-- /.block-flat -->
    </div>
</div>
{{/Block}}
{{Block "footer"}}
<script type="text/template" id="tmplAddVariableRow">
<tr>
	<td><input type="text" class="form-control" name="{k}" value=""></td>
	<td><input type="text" class="form-control" name="{v}" value=""></td>
	<td class="text-center">
    <button type="button" class="btn btn-danger" onclick="$(this).parent().parent().remove()">
      <i class="fa fa-times"></i>
    </button>
  </td>
</tr>
</script>
{{Modal "__TMPL__/request_placeholders.yaml"}}
<script>
function showRequestPlaceholders(){
  $('#request-placeholders-modal').niftyModal('show');
}
function insertAdvRewrite(){
  App.insertAtCursor($('#rewrite_rule')[0],'rewrite / {\n\
    regexp pattern\n\
    ext    extensions...\n\
    if     a cond b\n\
    if_op  [and|or]\n\
    to     destinations...\n\
}\n');
}
function insertCondForRewrite(obj){
  App.insertAtCursor($('#rewrite_rule')[0],$(obj).children('strong').text()+' ');
}
$(function(){
function addKVs(obj,k,v){
  var tmpl=$('#tmplAddVariableRow').html();
  tmpl=tmpl.replace(/\{k\}/g,k);
  tmpl=tmpl.replace(/\{v\}/g,v);
  $(obj).parent().before(tmpl);
}
$(document).on('click','#fastcgiAddEnvVarBtn',function(){
  addKVs(this,'fastcgi_env_k','fastcgi_env_v');
});
$(document).on('click','#headerAddVarBtn',function(){
  addKVs(this,'header_k','header_v');
});
$(document).on('click','#proxyHeaderDownstreamBtn',function(){
  addKVs(this,'proxy_header_downstream_k','proxy_header_downstream_v');
});
$(document).on('click','#proxyHeaderUpstreamBtn',function(){
  addKVs(this,'proxy_header_upstream_k','proxy_header_upstream_v');
});
$(document).on('click','#expiresMatchesBtn',function(){
  addKVs(this,'expires_match_k','expires_match_v');
});
$('[data-slide-settings]').each(function(){
  var eid='#'+$(this).attr('name')+'-settings';
  if($(eid).data('history'))$(eid).data('history',null);
});
App.iCheck('[data-slide-settings]','click',function(){
  var slide=$(this).data('slide-settings');
  if(slide==""){
    slide=$(this).val()=='1'?'show':'hide';
  }
  var eid='#'+$(this).attr('name')+'-settings';
  var history=window.location.pathname.match(/_add$/)?'':$.trim($(eid).html());
  if(slide=='show'){
    if(!history){
      if($(eid).data('history')){
        $(eid).html($(eid).data('history'));
      }else{
        $.get('/addon_form',{addon:$(this).attr('name')},function(r){
          $(eid).html(r);
        },'html');
      }
    }
    $(eid).removeClass('hide');
  }else{
    if(history)$(eid).data('history',history);
    $(eid).empty().addClass('hide');
  }
});
$('[data-slide-settings="show"]:checked').trigger('click');
$('#request-placeholders-modal').on('click','a[data-placeholder]',function(){
  App.insertAtCursor($('#rewrite_rule')[0],$(this).data('placeholder'));
  $('#request-placeholders-modal').niftyModal('hide');
});
});
</script>
{{/Block}}